from goods.models import GoodsCategory


def get_breadcrumb(category):
    """
    获取面包屑导航
    """
    # 创建响应对象
    breadcrumb = {
        'cat1': '',
        'cat2': '',
        'cat3': ''
    }
    # 判断面包屑等级
    if not category.parent:  # 查询一级
        breadcrumb['cat1'] = category
    elif category.subs.count() == 0:  # 查询三级，不能用not category.subs，因为category.subs查出来的空不能作为python判断的依据
        breadcrumb['cat1'] = category.parent.parent
        breadcrumb['cat2'] = category.parent
        breadcrumb['cat3'] = category
    else:  # 查询二级
        breadcrumb['cat1'] = category.parent
        breadcrumb['cat2'] = category
    # 返回数据
    return breadcrumb

